package com.example.servlet.manager;

import com.example.entity.Manager;
import com.example.service.ManagerService;
import com.example.service.impl.ManagerServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@WebServlet("/manager_flight")
public class mFlightServlet extends HttpServlet {
    private ManagerService service;
    private SqlSession sqlSession;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new ManagerServiceImpl(sqlSession);
    }

    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try (SqlSession newSession = MybatisUtil.getSession()) {
            ManagerService freshService = new ManagerServiceImpl(newSession);
            Manager manager = (Manager) req.getSession().getAttribute("manager");
            // 获取今天的日期
            LocalDate today = LocalDate.now();
            String todayStr = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            // 获取昨天的日期
            LocalDate yesterday = today.minusDays(1);
            String yesterdayStr = yesterday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            // 获取7天前的日期
            LocalDate lastWeek = today.minusDays(7);
            String lastWeekStr = lastWeek.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            int FlightCount1 = freshService.getFlightCountByDate(todayStr);
            int FlightCount2 = FlightCount1 - freshService.getFlightCountByDate(yesterdayStr);

            int delayFlightNum1 = freshService.getCancelFlightNum(todayStr);
            int delayFlightNum2 = delayFlightNum1 - freshService.getCancelFlightNum(yesterdayStr);

            int cancelFlightNum1 = freshService.getCancelFlightNum(todayStr);
            int cancelFlightNum2 = cancelFlightNum1 - freshService.getCancelFlightNum(yesterdayStr);

            int flightSpace1 = freshService.getFlightSpaceByDate(todayStr);
            int flightSpace2 = flightSpace1 - freshService.getFlightSpaceByDate(yesterdayStr);

            Context context = new Context();
            if (req.getSession().getAttribute("add_success") != null) {
                context.setVariable("add_success", true);
                req.getSession().removeAttribute("add_success");
            }
            if (req.getSession().getAttribute("add_failure") != null) {
                context.setVariable("add_failure", true);
                req.getSession().removeAttribute("add_failure");
            }
            if (req.getSession().getAttribute("delay_success") != null) {
                context.setVariable("delay_success", true);
                req.getSession().removeAttribute("delay_success");
            }
            if (req.getSession().getAttribute("delay_failure") != null) {
                context.setVariable("delay_failure", true);
                req.getSession().removeAttribute("delay_failure");
            }
            if (req.getSession().getAttribute("delete_success") != null) {
                context.setVariable("delete_success", true);
                req.getSession().removeAttribute("delete_success");
            }
            if (req.getSession().getAttribute("delete_failure") != null) {
                context.setVariable("delete_failure", true);
                req.getSession().removeAttribute("delete_failure");
            }
            if (req.getSession().getAttribute("edit_success") != null) {
                context.setVariable("edit_success", true);
                req.getSession().removeAttribute("edit_success");
            }
            if (req.getSession().getAttribute("edit_failure") != null) {
                context.setVariable("edit_failure", true);
                req.getSession().removeAttribute("edit_failure");
            }
            context.setVariable("managername", manager.getM_name());
            context.setVariable("flightList", freshService.getFlightList());
            context.setVariable("flightCount", FlightCount1);
            if (FlightCount2 <= 0) {
                FlightCount2 = -FlightCount2;
                context.setVariable("flightCountSign", "negative");
            } else {
                context.setVariable("flightCountSign", "positive");
            }
            context.setVariable("flightCountCompare", FlightCount2);
            context.setVariable("delayFlightNum", delayFlightNum1);
            if (delayFlightNum2 <= 0) {
                delayFlightNum2 = -delayFlightNum2;
                context.setVariable("delayFlightNumSign", "negative");
            } else {
                context.setVariable("delayFlightNumSign", "positive");
            }
            context.setVariable("delayFlightNumCompare", delayFlightNum2);
            context.setVariable("cancelFlightNum", cancelFlightNum1);
            if (cancelFlightNum2 <= 0) {
                cancelFlightNum2 = -cancelFlightNum2;
                context.setVariable("cancelFlightNumSign", "negative");
            } else {
                context.setVariable("cancelFlightNumSign", "positive");
            }
            context.setVariable("cancelFlightNumCompare", cancelFlightNum2);
            context.setVariable("flightSpace", flightSpace1);
            if (flightSpace2 <= 0) {
                flightSpace2 = -flightSpace2;
                context.setVariable("flightSpaceSign", "negative");
            } else {
                context.setVariable("flightSpaceSign", "positive");
            }
            context.setVariable("flightSpaceCompare", flightSpace2);
            ThymeleafUtil.process("manager_flight_manage.html", context, resp.getWriter());
        }
    }
}
